﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace VideoPlay.PhoneClient.Views
{
    public partial class SpinnerControl : UserControl
    {
        #region Fields

        public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register("IsSpinning", typeof(bool), typeof(SpinnerControl), new PropertyMetadata(OnIsSpinningPropertyChanged));

        public static readonly DependencyProperty StatusProperty = DependencyProperty.Register("Status", typeof(string), typeof(SpinnerControl), new PropertyMetadata(OnStatusPropertyChanged));

        #endregion

        #region Constructors

        public SpinnerControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        public bool IsSpinning
        {
            get { return (bool)GetValue(IsSpinningProperty); }
            set { SetValue(IsSpinningProperty, value); }
        }

        public string Status
        {
            get { return (string)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        #endregion

        #region Private Methods

        private static void OnIsSpinningPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            SpinnerControl spinnerControl = dependencyObject as SpinnerControl;

            if (spinnerControl != null)
            {
                if (spinnerControl.IsSpinning)
                {
                    spinnerControl.StoryBoardAnimateSpinner.Begin();
                }
                else
                {
                    spinnerControl.StoryBoardAnimateSpinner.Stop();
                }

                spinnerControl.Visibility = spinnerControl.IsSpinning ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        private static void OnStatusPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            SpinnerControl spinnerControl = dependencyObject as SpinnerControl;

            if (spinnerControl != null)
            {
                spinnerControl.textBlockStatus.Text = spinnerControl.Status;
            }
        }

        #endregion
    }
}
